package com.chenyue.cm.common.controller;


import com.alibaba.fastjson.JSONObject;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.domain.JdBanner;
import com.chenyue.cm.common.domain.JdBannerExample;
import com.chenyue.cm.common.service.JdBannerService;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.live.domain.JdLiveRecord;
import com.chenyue.cm.match.domain.FormatLiveRecord;
import com.chenyue.cm.match.service.FormatLiveRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdBanner")
public class JdBannerController {

    private final JdBannerService jdBannerService;

    private final FormatLiveRecordService formatLiveRecordService;

    public JdBannerController(JdBannerService jdBannerService, FormatLiveRecordService formatLiveRecordService) {
        this.jdBannerService = jdBannerService;
        this.formatLiveRecordService = formatLiveRecordService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdBanner> result = jdBannerService.getPageBean(pageInfo, JdBanner.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdBanner vo) {
        dealParam(vo);
        boolean success = jdBannerService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdBanner vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            dealParam(vo);
            int success = jdBannerService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * 处理参数
     * @param vo
     */
    private void dealParam(JdBanner vo){
        if(vo.getTransType() != null){
            if(StringUtils.isNotBlank(vo.getTransParam())){
                if(vo.getTransType() == 2){//直播间
                    JSONObject jsonObj = new JSONObject();
                    jsonObj.put("liveRecordId",vo.getTransParam());
                    //添加主播类型 1普通2赛事
                    FormatLiveRecord formatLiveRecord = formatLiveRecordService.getById(vo.getTransParam());
                    if(formatLiveRecord != null){
                        jsonObj.put("type",formatLiveRecord.getType());
                    }
                    vo.setTransParam(jsonObj.toJSONString());
                }
                if("3,4,5".contains(vo.getTransType().toString())){//直播间
                    JSONObject jsonObj = new JSONObject();
                    jsonObj.put("id",vo.getTransParam());
                    vo.setTransParam(jsonObj.toJSONString());
                }
            }
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdBannerExample aExample = new JdBannerExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdBannerService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdBannerService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

}
